package com.jyx.splider.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.jyx.pinyin.PingYinUtil;
import com.jyx.splider.img.Image;

/**
 * @author  	������
 * @email       jiangyongxing@myzczk.com
 * @company		www.urbanspace.cn
 * @date 		2016��6��2�� ����8:52:52 
 * @version 	1.0
 */
public class SpliderUtil {
private static Map<String,String> urls = new HashMap<String,String>();
	
	static{
		urls.put("fresh","http://www.doubanmeizi.com/category/qingxin/");
		urls.put("gif","http://www.doubanmeizi.com/category/gif/");
		urls.put("qq","http://localhost:8081/zczkWeb/countArea.html");
		urls.put("menu","http://www.myzczk.com/cms/legislation/index.htm");
	}
	
	public static void nextQuery(String url) throws IOException{
		Document document = Jsoup.connect(url)
				.userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:47.0) Gecko/20100101 Firefox/47.0")
				.data()
				.timeout(5000)
				.get();
		
		// 针对HTML字符串转化
		/*String html = "<div><p>Lorem ipsum.</p><p>asdf<div>asdf";
		Document doc = Jsoup.parse(html);//.parseBodyFragment(html);
		Element body = doc.body();
		System.out.println(body.children().select("h2"));
		body.children().subList(0, 1);*/
		// 获取标题和内容
		Elements h2s = document.select("h2");
		Elements content = document.getElementById("content").child(0).children();//.getAllElements();//contentDivs.get(0).getAllElements();
		int fromIndex = 0;
		for (Element h2 : h2s) {
			int toIndex = content.indexOf(h2);
			if(toIndex == 0){
				continue;
			}
			if(toIndex == -1){
				return ;
			}
			System.out.println(content.subList(fromIndex, toIndex));
			fromIndex = toIndex;		
		}
		
		
		
		/*System.out.println(contentDivs.get(0).getAllElements().indexOf(h2s.get(0)));//.contains(h2s.get(0)));//.select("h2") .childNodeSize(); 
		System.out.println(contentDivs.lastIndexOf(h2s.get(0)));
		for (Element h2 : h2s) {
			System.out.println(contentDivs.indexOf(h2));
		}*/
		
		
		/*for (Element element : elements) {
			element.in
			System.out.println(element.text());
		}*/
		/*
		document.getElementsByTag("h3");
		Elements bodys = document.getElementsByClass("use-content");
		for (Element body : bodys) {
			Elements nodes = body.getAllElements();
			for (Element node : nodes) {
				if(node.tagName().equals("h2")){
					System.out.println("\t"+node.text());
				}else if(node.tagName().equals("strong")){
					if(!node.parent().parent().tagName().equals("h2") && !node.text().equals(""))
					System.out.println("\t\t"+node.text());
				}
			}
		}*/
	}
	
	/**
	 * 查询菜单
	 */
	public static void queryMen(){
		try {
			Document document = Jsoup.connect(urls.get("menu"))
				.userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:47.0) Gecko/20100101 Firefox/47.0")
				.data()
				.timeout(5000)
				.get();
			Elements bodyDiv = document.getElementsByClass("v3-lemmaList");
			for (Element body : bodyDiv) {
				Elements lis = body.getElementsByTag("li");
				for (Element li : lis) {
					Elements links = li.getElementsByTag("a");
					for (Element link : links) {
						String href = link.attr("href");
						String text = link.text();
						System.out.println(text+":\t"+href);
						nextQuery(href);
					}
				}
			}
			/*for (Element body : bodys) {
				Elements h = body.getElementsByTag("h2");
				for (Element h2 : h) {
					System.out.println(h2.text());
					
				}
			}*/
			
		} catch (IOException e) {
			e.printStackTrace();
			
		}
	}
	
	
	
	public static void main(String[] args) {
		// queryMen();
		
		try {
			nextQuery("http://myzczk.com/cms/legislation/detail/d994c631b15711e6a7228038bc0bb04f.htm");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	
	
	public static List<Image> queryImageList(){
		List<Image> images = new ArrayList<Image>();
		try {
			StringBuffer content = new StringBuffer();
			Document doc = Jsoup.connect(urls.get("qq")).userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:47.0) Gecko/20100101 Firefox/47.0").data().timeout(5000).post();
			Elements tags =  doc.getElementsByTag("strong");
			for (Element element : tags) {
				System.out.println(element.attr("value")+"==="+element.html()+"==="+PingYinUtil.getPinYinHeadChar(element.html()));
				content.append("INSERT INTO `sys_country` (`ID`, `C_CREATE_BY`, `C_CREATE_DATE`, `C_UPDATE_BY`, `C_UPDATE_DATE`, `C_DELETE_FLAG`, `C_NAME`, `C_CODE`, `C_PY_CODE`) VALUES ('")
					.append(UUID.randomUUID().toString().replace("-",""))
					.append("', 'admin', '2016-07-07 12:57:16', 'admin', '2016-07-07 12:57:17', 0, '")
					.append(element.html())
					.append("', '")
					.append(element.attr("value"))
					.append("', '")
					.append(PingYinUtil.getPinYinHeadChar(element.html()))
					.append("');\r");
			}
			 File file = new File("d:/country.txt");
			  try (FileOutputStream fop = new FileOutputStream(file)) {
			   if (!file.exists()) {
			    file.createNewFile();
			   }
			   byte[] contentInBytes = content.toString().getBytes();
			   fop.write(contentInBytes);
			   fop.flush();
			   fop.close();
			  } catch (IOException e) {
			   e.printStackTrace();
			  }


		} catch (IOException e) {
			e.printStackTrace();
		}
		return images;
	}
	
}
